package com.wctj.api.base.utils.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.wctj.api.base.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author: wangqi
 * @description:
 * @date: Created in 2017/10/25 17:16
 */
@Slf4j
public class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {

    public LocalDateTimeDeserializer() {
        this(null);
    }

    public LocalDateTimeDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctx)
            throws IOException {
        String dateTimeString = jp.readValueAs(String.class);
        if (dateTimeString == null || dateTimeString.trim().isEmpty()) {
            return null;
        }
        return TimeUtils.parseDateTime2LocalDateTime(dateTimeString);
    }
}
